#Setup the workspace
rm(list=ls())
#Setup the workspace
rm(list=ls())
library(rjags)
library(bayesplot)
# Hippo survival data
y=c(0,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0)
#JAGS data list
data=list(
y=y,
N=length(y)
)
#MCMC inputs
n.chains=2
n.adapt=100
n.iter=1000
thin=2
burn=500
# Model Parameters to save values of
parms=c("p")
# Setup the Model
jm=jags.model(file="model.jags.r", data=data)
# Update the model with the burnin
update(jm, n.iter=burn,n.adapt=n.adapt)
#Fit the modedl
post=coda.samples(jm, variable.names=parms, n.chains=n.chains,n.iter=n.iter, thin=thin)
#Basic Plot of posterior
hist(as.matrix(post))
#Look at chains
#Plot all chains MCMC iterations
color_scheme_set("blue")
mcmc_trace(post)
#Fancy plot of posterior
mcmc_areas(as.matrix(post))
#MCMC inputs
n.chains=2
n.adapt=1000
n.iter=5000
thin=2
burn=1000
# Model Parameters to save values of
parms=c("p")
# Setup the Model
jm=jags.model(file="model.jags.r", data=data)
# Update the model with the burnin
update(jm, n.iter=burn,n.adapt=n.adapt)
#Fit the modedl
post=coda.samples(jm, variable.names=parms, n.chains=n.chains,n.iter=n.iter, thin=thin)
#Basic Plot of posterior
hist(as.matrix(post))
#Look at chains
#Plot all chains MCMC iterations
color_scheme_set("blue")
mcmc_trace(post)
#Fancy plot of posterior
mcmc_areas(as.matrix(post))
#Posterior mean and median
mean(as.matrix(post))
median(as.matrix(post))
#95% Credible Intervals
quantile(as.matrix(post),probs=c(0.025,0.975))
#Look at chains
#Plot all chains MCMC iterations
color_scheme_set("blue")
mcmc_trace(post)
#Look at chains
#Plot all chains MCMC iterations
# color_scheme_set("blue")
mcmc_trace(post)
?mcmc_trace
#Look at chains
#Plot all chains MCMC iterations
color_scheme_set("viridis")
mcmc_trace(post)
#MCMC inputs
n.chains=3
n.adapt=1000
n.iter=5000
thin=2
burn=1000
# Model Parameters to save values of
parms=c("p")
# Setup the Model
jm=jags.model(file="model.jags.r", data=data)
# Update the model with the burnin
update(jm, n.iter=burn,n.adapt=n.adapt)
#Fit the modedl
post=coda.samples(jm, variable.names=parms, n.chains=n.chains,n.iter=n.iter, thin=thin)
#Basic Plot of posterior
hist(as.matrix(post))
#Look at chains
#Plot all chains MCMC iterations
color_scheme_set("viridis")
mcmc_trace(post)
#Fancy plot of posterior
mcmc_areas(as.matrix(post))
#Posterior mean and median
mean(as.matrix(post))
post
mcmc_trace(post)
length(post)
n.chains
?coda.samples
?jags.model
# Setup the Model
jm=jags.model(file="model.jags.r", data=data,n.chains=n.chains)
# Update the model with the burnin
update(jm, n.iter=burn,n.adapt=n.adapt)
#Fit the modedl
post=coda.samples(jm, variable.names=parms, n.iter=n.iter, thin=thin)
#Basic Plot of posterior
hist(as.matrix(post))
#Look at chains
#Plot all chains MCMC iterations
color_scheme_set("viridis")
mcmc_trace(post)
#Fancy plot of posterior
mcmc_areas(as.matrix(post))
#Posterior mean and median
mean(as.matrix(post))
median(as.matrix(post))
#95% Credible Intervals
quantile(as.matrix(post),probs=c(0.025,0.975))
?jags.model
# Setup the Model
jm=jags.model(file="model.jags.r", data=data,n.chains=n.chains,n.adapt=n.adapt)
# Update the model with the burnin
update(jm, n.iter=burn)
#Fit the modedl
post=coda.samples(jm, variable.names=parms, n.iter=n.iter, thin=thin)
#Basic Plot of posterior
hist(as.matrix(post))
#Look at chains
#Plot all chains MCMC iterations
color_scheme_set("viridis")
mcmc_trace(post)
#Fancy plot of posterior
mcmc_areas(as.matrix(post))
#Posterior mean and median
mean(as.matrix(post))
#Setup the workspace
rm(list=ls())
library(rstanarm)
library(bayesplot)
#Hippo survival data
y=c(0,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0)
#Create a datafame
dat=data.frame(y=y)
#Hippo survival data
y=c(0,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0)
#Create a datafame
dat=data.frame(y=y)
#Fit model in stan_glm function. Note, the lack of a prior specifiction.
post1 <- stan_glm(y~1, data = dat,
family = binomial(link = "logit"))
#Summarize the estimate coefs and predicted/fitted values
plot(post1)
summary(post1)
post1$coefficients
#Plot all chains MCMC iterations
color_scheme_set("virdris")
#Plot all chains MCMC iterations
color_scheme_set("viridis")
mcmc_trace(post1)
#Posterior mean and median
mean(as.matrix(post1))
median(as.matrix(post1))
#95% Credible Intervals
quantile(as.matrix(post1),probs=c(0.025,0.975))
plogis(post1$coefficients)
post1$fitted.values
predict(post1)
predict(post1,type="response")
#Posterior mean and median
mean(as.matrix(post1))
median(as.matrix(post1))
#95% Credible Intervals
quantile(as.matrix(post1),probs=c(0.025,0.975))
#Summarize the estimate coefs and predicted/fitted values
plot(post1)
summary(post1)
?posterior_predict
posterior_predict(post1)
posterior_predict(post1,draws = TRUE)
extract(post1)
rstanarm::posterior_epred(post1)
dim(rstanarm::posterior_epred(post1))
rstanarm::posterior_epred(post1)
dim(rstanarm::posterior_epred(post1))
length(y)
# posterior predictions of the probability
# each column is for each observations (all same)
# as this is the intercept model. Rows are the mcmc iterations
preds=rstanarm::posterior_epred(post1)
hist(preds[,1])
